package tr.gen._3bit.java._2k1s.dao.service;

import java.util.List;

import javax.persistence.Query;

import tr.gen._3bit.java._2k1s.dao.model.Measurement;
import tr.gen._3bit.java._2k1s.dao.model.Node;
import tr.gen._3bit.java._2k1s.dao.model.SerialInterface;

public class MeasurementServiceImpl extends ServiceImpl<Measurement> implements
		MeasurementService {

	@Override
	protected Class<Measurement> getPersistanceClass() {
		return Measurement.class;
	}

	@Override
	public Measurement getLastByNode(Node node) {
		Query query = getEntityManager().createQuery(
				"select m FROM " + getPersistanceClass().getName()
						+ " m where m.node = :node order by m.date desc");
		query.setParameter("node", node);
		query.setMaxResults(1);
		return (Measurement) query.getSingleResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Measurement> getLastMeasurements(
			SerialInterface serialInterface, Node node, Integer count) {
		Query query = getEntityManager()
				.createQuery(
						"select m FROM "
							+ getPersistanceClass().getName()
							+ " m where m.node = :node and " +
							"m.serialInterface = :serialInterface " +
							"order by m.date desc");
		query.setParameter("node", node);
		query.setParameter("serialInterface", serialInterface);
		query.setMaxResults(count);
		return query.getResultList();
	}
}
